package com.wait.onlinepicturecloud.interfaces.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wait.onlinepicturecloud.infrastructure.annotation.AuthCheck;
import com.wait.onlinepicturecloud.infrastructure.common.DeleteRequest;
import com.wait.onlinepicturecloud.domain.user.constant.UserConstant;
import com.wait.onlinepicturecloud.infrastructure.exception.BusinessException;
import com.wait.onlinepicturecloud.infrastructure.manage.upload.FileManager;
import com.wait.onlinepicturecloud.infrastructure.manage.upload.file.UploadPictureResult;

import com.wait.onlinepicturecloud.domain.user.entity.User;
import com.wait.onlinepicturecloud.interfaces.assembler.UserAssembler;
import com.wait.onlinepicturecloud.interfaces.vo.user.LoginUserVO;
import com.wait.onlinepicturecloud.infrastructure.common.BaseResponse;
import com.wait.onlinepicturecloud.infrastructure.common.ResultUtils;
import com.wait.onlinepicturecloud.infrastructure.exception.ErrorCode;
import com.wait.onlinepicturecloud.infrastructure.exception.ThrowUtils;
import com.wait.onlinepicturecloud.interfaces.vo.user.UserVO;
import com.wait.onlinepicturecloud.application.service.UserApplicationService;
import com.wait.onlinepicturecloud.interfaces.dto.user.*;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author with葡萄成熟时
 * @version 1.0
 * 用户相关接口
 */
@RestController
@RequestMapping("/user")

public class UserController {

    @Resource
    private UserApplicationService userApplicationService;
    @Resource
    private FileManager fileManager;

    /**
     * description 用户注册接口
     * author Wait葡萄成熟时
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        Long result = userApplicationService.userRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    /**
     * description 用户登录接口
     * author Wait葡萄成熟时
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        LoginUserVO loginUserVO = userApplicationService.userLogin(userLoginRequest, request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * description 从请求头获取当前登录用户接口
     * author Wait葡萄成熟时
     */
    @ApiOperation("从请求头获取当前登录用户")
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userApplicationService.getLoginUser(request);
        return ResultUtils.success(userApplicationService.getLoginUserVO(loginUser));
    }

    /**
     * description 用户登录注销接口
     * author Wait葡萄成熟时
     */
    @ApiOperation("用户登录注销")
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        boolean result = userApplicationService.userLogout(request);
        return ResultUtils.success(result);
    }


    /**
     * description 创建用户
     * author Wait葡萄成熟时
     * date 上午12:16 2025/3/11
     */
    @ApiOperation("创建用户")
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.PARAMS_ERROR);
        User user = UserAssembler.toUserEntity(userAddRequest);
        return ResultUtils.success(userApplicationService.addUser(user));
    }

    /**
     * description 根据 id 获取用户（仅管理员）
     * author Wait葡萄成熟时
     * date 上午12:16 2025/3/11
     */
    @ApiOperation("根据 id 获取用户（仅管理员）")
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userApplicationService.getUserById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * description 根据 id 获取包装类
     * author Wait葡萄成熟时
     * date 上午12:16 2025/3/11
     */
    @ApiOperation("根据 id 获取包装类")
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(long id) {
        BaseResponse<User> response = getUserById(id);
        User user = response.getData();
        return ResultUtils.success(userApplicationService.getUserVO(user));
    }

    /**
     * description 删除用户
     * author Wait葡萄成熟时
     * date 上午12:16 2025/3/11
     */
    @ApiOperation("删除用户")
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userApplicationService.deleteUser(deleteRequest);
        return ResultUtils.success(b);
    }

    /**
     * description 更新用户
     * author Wait葡萄成熟时
     * date 上午12:16 2025/3/11
     */
    @ApiOperation("更新用户")
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        if (userUpdateRequest == null || userUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = UserAssembler.toUserEntity(userUpdateRequest);
        userApplicationService.updateUser(user);
        return ResultUtils.success(true);
    }

    /**
     * description 给用户修改
     * author Wait葡萄成熟时
     */
    @ApiOperation("更新用户")
    // @PostMapping注解表示该方法是一个POST请求，请求路径为/edit
    @PostMapping("/edit")
    // 返回一个BaseResponse<Boolean>类型的数据
    public BaseResponse<Boolean> editUser(@RequestBody UserEditRequest userEditRequest) {
        if (userEditRequest == null || userEditRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = UserAssembler.toUserEntity(userEditRequest);
        userApplicationService.updateUser(user);
        return ResultUtils.success(true);
    }
    /**
     * description 分页获取用户封装列表（仅管理员）
     * author Wait葡萄成熟时
     * date 上午12:16 2025/3/11
     */
    @ApiOperation("分页获取用户封装列表（仅管理员）")
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Page<UserVO> userVOPage = userApplicationService.listUserVOByPage(userQueryRequest);
        return ResultUtils.success(userVOPage);
    }

    /**
     * description 上传头像
     * author Wait葡萄成熟时
     */
    @ApiOperation("上传头像")
    @PostMapping("/upload")
    /*@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)*/
    public BaseResponse<UploadPictureResult> uploadPictureUser(
            @RequestPart("file") MultipartFile multipartFile,
            HttpServletRequest request) {
        return ResultUtils.success(userApplicationService.uploadAvatar(multipartFile));
    }
}

